//
//  CommentCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 15/12/23.
//  Copyright © 2015年 game. All rights reserved.
//

#import "CommentCell.h"
#import "PassportModel.h"
#import "ChangyanSDK.h"
#import "AppDelegate.h"
#import "ZanButton.h"
#import "UILabel+BoundRect.h"
#import "AppDelegate.h"
#import "Masonry.h"
#import "UIButton+WebCache.h"
#define AddsubView(view) [self.contentView addSubview:view]

@interface CommentCell ()
@property (nonatomic,strong) PassportModel *passport;
@property (nonatomic,strong) UIView *line;
@property (nonatomic,assign) BOOL isZan;

@property (nonatomic,strong) AppDelegate *app;

@end

@implementation CommentCell
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {

        self.selectionStyle = UITableViewCellSelectionStyleNone;

    }
    return self;
}


-(void)setModel:(CommetModel *)model{

    _model = model;
    
    self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    _passport = _model.passport;

    //头像
    [self.UserHead sd_setImageWithURL:[NSURL URLWithString:self.passport.img_url] forState:UIControlStateNormal];
    
    //用户名
    self.userName.text = self.passport.nickname;
    
    //等级
    self.grade.text = [NSString stringWithFormat:@"Lv.%@",_model.userScore[@"level"]];
    
    //时间
self.time.text = [Manager GetTimeWhithTimesMark:_model.create_time.integerValue/1000];
    
    //评论
    self.comment.text =_model.content;

    //回复按钮
    [self.replyBnt setTitle:@"回复" forState:UIControlStateNormal];
  
    if (_model.isZan) {
        self.PramieBnt.selected = YES;
        NSInteger count = _model.support_count.integerValue+1;
        
        [self.PramieBnt setTitle:[NSString stringWithFormat:@"%ld",(long)count] forState:UIControlStateNormal];
    }else{
        self.PramieBnt.selected = NO;
        [self.PramieBnt setTitle:model.support_count forState:UIControlStateNormal];
    }

     CGSize Comnetsize = CGSizeMake(SCREEN_WIDTH-(15+20+5+10+10), 0);
    CGRect ComnetRct = [self.comment boundingRectWithInitSize:Comnetsize];
    if (model.comments.count==0) {//如果没有评论回复
  
        self.cellhight= ComnetRct.size.height+80;
        self.replyText.text = nil ;
        
    }else{

        CommetModel *com2 = [[CommetModel alloc]initWithDictionary:model.comments.firstObject error:nil ];
        NSString *replyStr = [NSString stringWithFormat:@"  %@",com2.content];
         self.replyText.text = replyStr;
        CGRect ReplayComRct = [self.replyText boundingRectWithInitSize:Comnetsize];;
       
        self.cellhight = ComnetRct.size.height+ReplayComRct.size.height+100;
    }
    
    self.line.backgroundColor = [UIColor lightGrayColor];
}

-(CGFloat)cellhight{
    if (!_cellhight) {
        
    }
    return _cellhight;
}

/* 头像 */
-(UIButton *)UserHead{
    if (!_UserHead) {
        _UserHead = [UIButton buttonWithType:UIButtonTypeCustom];
        _UserHead.frame = CGRectMake(15, 10, 30, 30);
        radius(_UserHead, 15);
        _UserHead.userInteractionEnabled = YES;
        _UserHead.adjustsImageWhenDisabled = NO;
        _UserHead.adjustsImageWhenHighlighted = NO;
        AddsubView(_UserHead);
        [_UserHead addTarget:self action:@selector(TapClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _UserHead;
}

/* 用户名 */
-(UILabel *)userName{
    if (!_userName) {
        _userName = [[UILabel alloc]init];
        _userName.textColor = mainColor;
        _userName.userInteractionEnabled = YES;
        AddsubView(_userName);
        [_userName mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_UserHead.mas_right).offset(5);
            make.centerY.equalTo(_UserHead.mas_centerY);
        }];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(TapClick:)];
        [_userName addGestureRecognizer:tap];
    }
    return _userName;
}

/*等级*/
-(UILabel *)grade{
    if (!_grade) {
        
        _grade = [[UILabel alloc]init];
        _grade.textColor = [UIColor whiteColor];
        _grade.backgroundColor = mainColor;
        _grade.font = font(10);
        _grade.textAlignment = NSTextAlignmentCenter;
        AddsubView(_grade);
        
        [_grade mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_userName.mas_right).offset(5);
            make.centerY.equalTo(_UserHead.mas_centerY);
            make.height.equalTo(@15);
            make.width.equalTo(@30);
        }];
    }
        return _grade;
}


/*评论*/
-(UILabel *)comment{
    if (!_comment) {
        _comment = [[UILabel alloc]init];
        _comment.numberOfLines = 0;
        _comment.font = font(SCREEN_WIDTH*0.045);
        _comment.alpha = 0.6;
        _comment.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(ReplyClick)];
        [_comment addGestureRecognizer:tap];
        
        AddsubView(_comment);
        [_comment mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(_time.mas_top).offset(-10);
            make.right.equalTo(@-10);
            make.left.equalTo(_userName.mas_left);
        }];

    }
        return _comment;
}

/*评论回复*/
-(UILabel *)replyText{
    if (!_replyText) {
        _replyText = [[UILabel alloc]init];
        _replyText.backgroundColor = RGBA(242, 242, 242,1);
        _replyText.numberOfLines = 0;
        _replyText.font= font(SCREEN_WIDTH*0.045);
        _replyText.textColor = RGBA(140, 140, 140, 1);

        AddsubView(_replyText);
        [_replyText mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_userName.mas_left);
            make.right.equalTo(@-10);
            make.bottom.equalTo(_comment.mas_top).offset(-10);
        }];
    }
        return _replyText;
}

/*时间*/
-(UILabel *)time{
    if (!_time) {
        _time = [[UILabel alloc]init];
        _time.font= font(SCREEN_WIDTH*0.037);
        _time.textColor = [UIColor grayColor];
        _time.alpha = 0.4;
        AddsubView(_time);
        [_time mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(@-10);
            make.left.equalTo(_userName.mas_left);
        }];
    }
        return _time;
}

/*评论回复button*/
-(UIButton *)replyBnt{
    if (!_replyBnt) {
       
        _replyBnt = [self CreatBntWhithType:UIButtonTypeCustom AndBntImgName:@"btn-reply" WhithFont:15];
        _replyBnt.titleEdgeInsets = UIEdgeInsetsMake(0,3,0, -5);
        [_replyBnt addTarget:self action:@selector(ReplyClick) forControlEvents:UIControlEventTouchUpInside];
        
        [_replyBnt mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(@-15);
            make.centerY.equalTo(_time.mas_centerY);
        }];
    }
        return _replyBnt;
}

/*点赞button*/
-(UIButton *)PramieBnt{
    if (!_PramieBnt) {
        _PramieBnt = [self CreatBntWhithType:UIButtonTypeCustom AndBntImgName:@"btn-praise" WhithFont:15];
        [_PramieBnt setImage:[UIImage imageNamed:@"btn-reply-green"] forState:UIControlStateSelected | UIControlStateHighlighted];
        [_PramieBnt setImage:[UIImage imageNamed:@"btn-reply-green"] forState:UIControlStateSelected];
        _PramieBnt.titleEdgeInsets = UIEdgeInsetsMake(0,3,0, -5);
        [_PramieBnt addTarget:self action:@selector(pramieBtnLClick:) forControlEvents:UIControlEventTouchUpInside];
        [_PramieBnt mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(_replyBnt.mas_left).offset(-10);
            make.centerY.equalTo(_time.mas_centerY);
        }];
    }
        return _PramieBnt;
}

-(void)pramieBtnLClick:(UIButton *)button{
   
    if (!button.selected) {
        button.selected = YES;
       
        NSInteger count = button.titleLabel.text.integerValue+1;
        
        [button setTitle:[NSString stringWithFormat:@"%ld",(long)count] forState:UIControlStateNormal];
        
        [UIView animateWithDuration:0.5 animations:^{
             self.model.isZan = YES;
            button.imageView.transform = CGAffineTransformScale(button.imageView.transform, 0.65, 0.65);
        } completion:^(BOOL finished) {
            
        }];
        
    }
    
    if ([self.deleget respondsToSelector:@selector(ComentPramClick:)]) {
        [self.deleget ComentPramClick:self.model.comment_id];
    }
}

/* 线 */
-(UIView *)line{
    if (!_line) {
    _line = [[UIView alloc]initWithFrame:CGRectMake(0, _cellhight-1, screen_width, 0.5)];
        AddsubView(_line);
        
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(self.contentView);
            make.height.equalTo(@0.5);
        }];
    }
        return _line;
}


/**
 *  点击头像与用户名 调用代理
 */
-(void)TapClick:(UITapGestureRecognizer *)tap{
    
    [self.deleget HeadAndNameClickWhithId:_passport.isv_refer_id];
}


-(UIButton *)CreatBntWhithType:(UIButtonType)type AndBntImgName:(NSString *)string WhithFont:(CGFloat )font
{
    UIButton *bnt=[UIButton buttonWithType:type];
    [bnt setImage:[UIImage imageNamed:string] forState:UIControlStateNormal];
    [bnt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    bnt.titleLabel.font = font(font);
    AddsubView(bnt);
    return bnt;
}

-(void)ReplyClick{
    
    [self.deleget CommentReply:_model.comment_id UaerName:self.passport.nickname Row:self.RowOfcell];
}

@end